import request from '@/utils/request'

// 权限接口类型定义
export interface Permission {
  id: number
  name: string
  code: string
  description: string
  status: number
  created_at: string
  updated_at: string
}

export interface PermissionCreateRequest {
  name: string
  code: string
  description?: string
  status?: number
}

export interface PermissionUpdateRequest {
  name: string
  code: string
  description?: string
  status?: number
}

export interface PermissionListParams {
  page?: number
  page_size?: number
  keyword?: string
}

export interface PermissionListResponse {
  list: Permission[]
  total: number
  page: number
  page_size: number
}

// 获取权限列表
export function getPermissionList(params?: PermissionListParams) {
  return request<PermissionListResponse>({
    url: '/permissions',
    method: 'get',
    params
  })
}

// 获取所有权限（用于角色分配）
export function getAllPermissions() {
  return request<Permission[]>({
    url: '/permissions/all',
    method: 'get'
  })
}

// 获取权限详情
export function getPermission(id: number) {
  return request<Permission>({
    url: `/permissions/${id}`,
    method: 'get'
  })
}

// 创建权限
export function createPermission(data: PermissionCreateRequest) {
  return request<Permission>({
    url: '/permissions',
    method: 'post',
    data
  })
}

// 更新权限
export function updatePermission(id: number, data: PermissionUpdateRequest) {
  return request<Permission>({
    url: `/permissions/${id}`,
    method: 'put',
    data
  })
}

// 删除权限
export function deletePermission(id: number) {
  return request({
    url: `/permissions/${id}`,
    method: 'delete'
  })
}

// 更新权限状态
export function updatePermissionStatus(id: number, status: number) {
  return request({
    url: `/permissions/${id}/status`,
    method: 'put',
    data: { status }
  })
}
